package com.xul.crowd.controller;

import com.xul.crowd.api.MysqlRemoteService;
import com.xul.crowd.constant.CrowdConstant;
import com.xul.crowd.util.ResultEntity;
import com.xul.entity.vo.AddressVO;
import com.xul.entity.vo.MemberLoginVo;
import com.xul.entity.vo.OrderProjectVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author xul
 * @create 2021-08-01 16:58
 */
@Controller
public class OrderConsumerController {

    @Autowired
    private MysqlRemoteService mysqlRemoteService;

    private Logger logger = LoggerFactory.getLogger(OrderConsumerController.class);

    /**
     * 根据returnId查询回报信息
     *
     * @param projectId
     * @param returnId
     * @param session
     * @return
     */
    @RequestMapping("confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(@PathVariable Integer projectId,
                                        @PathVariable Integer returnId,
                                        HttpSession session) {
        ResultEntity<OrderProjectVO> resultEntity = mysqlRemoteService.getOrderProjectRemote(projectId, returnId);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            OrderProjectVO orderProjectVO = resultEntity.getData();
            session.setAttribute("orderProjectVO", orderProjectVO);
        }

        return "confirm-return";

    }

    /**
     * 获取到确认订单的信息
     *
     * @param returnCount
     * @param session
     * @return
     */
    @RequestMapping("confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable Integer returnCount,
                                       HttpSession session) {
        // 1.把把接收到的回报数量存入到session中
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute("orderProjectVO", orderProjectVO);
        // 2.获取当前已近登录的用户id
        MemberLoginVo memberLoginVo = (MemberLoginVo) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer memberId = memberLoginVo.getId();
        // 3.查询目前的收获地址数据
        ResultEntity<List<AddressVO>> resultEntity = mysqlRemoteService.getAddressVORemote(memberId);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            List<AddressVO> addressVOList = resultEntity.getData();
            session.setAttribute("addressVOList", addressVOList);
        }
        return "confirm-order";
    }

    /**
     * 保存会员地址信息
     *
     * @param addressVO
     * @param session
     * @return
     */
    @RequestMapping("save/address")
    public String saveAddress(AddressVO addressVO, HttpSession session) {
        // 1.执行地址信息的保存
        ResultEntity<String> resultEntity = mysqlRemoteService.saveAddressRemote(addressVO);
        logger.debug("地址保存的处理结果:" + resultEntity);
        // 2.从session域中获取orderProjectVO对象
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        // 3.从orderProjectVO对象中获取ReturnCount
        Integer returnCount = orderProjectVO.getReturnCount();
        // 4.重定向到指定的地址 重新进入确认订单页面
        return "redirect:http://localhost:/order/confirm/order/" + returnCount;
    }
}
